<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="135px">
      <el-form-item label="地区" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入地区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工商注册地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入工商注册地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否在化工园区内" prop="isInPark">
        <el-input
          v-model="queryParams.isInPark"
          placeholder="请输入是否在化工园区内"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统一社会信用代码" prop="socialId">
        <el-input
          v-model="queryParams.socialId"
          placeholder="请输入统一社会信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业人数" prop="workerNum">
        <el-input
          v-model="queryParams.workerNum"
          placeholder="请输入企业人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['factorymanagement:factoryInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['factorymanagement:factoryInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['factorymanagement:factoryInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['factorymanagement:factoryInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="factoryInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="地区" align="center" prop="area" />
      <el-table-column label="工商注册地址" align="center" prop="address" />
      <el-table-column label="营业执照经营范围" align="center" prop="bussinessScope" />
      <el-table-column label="是否在化工园区内" align="center" prop="isInPark" />
      <el-table-column label="统一社会信用代码" align="center" prop="socialId" />
      <el-table-column label="企业人数" align="center" prop="workerNum" />
      <el-table-column label="主要产品及生产规模" align="center" prop="mainProduct" />
      <el-table-column label="主要进口化学品" align="center" prop="mainChemistry" />
      <el-table-column label="企业建筑面积" align="center" prop="structureArea" />
      <el-table-column label="危险化学品库房面积" align="center" prop="chemistryArea" />
      <el-table-column label="储罐和容器总容积" align="center" prop="containerArea" />
      <el-table-column label="应急服务电话" align="center" prop="emergencyPhone" />
      <el-table-column label="安全值班电话" align="center" prop="securityPhone" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['factorymanagement:factoryInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['factorymanagement:factoryInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业信息登记对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="地区" prop="area">
          <el-input v-model="form.area" placeholder="请输入地区" />
        </el-form-item>
        <el-form-item label="工商注册地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入工商注册地址" />
        </el-form-item>
        <el-form-item label="营业执照经营范围" prop="bussinessScope">
          <el-input v-model="form.bussinessScope" placeholder="请输入营业执照经营范围" />
        </el-form-item>
        <el-form-item label="是否在化工园区内" prop="isInPark">
          <el-input v-model="form.isInPark" placeholder="请输入是否在化工园区内" />
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="socialId">
          <el-input v-model="form.socialId" placeholder="请输入统一社会信用代码" />
        </el-form-item>
        <el-form-item label="企业人数" prop="workerNum">
          <el-input v-model="form.workerNum" placeholder="请输入企业人数" />
        </el-form-item>
        <el-form-item label="主要产品及生产规模" prop="mainProduct">
          <el-input v-model="form.mainProduct" placeholder="请输入主要产品及生产规模" />
        </el-form-item>
        <el-form-item label="主要进口化学品" prop="mainChemistry">
          <el-input v-model="form.mainChemistry" placeholder="请输入主要进口化学品" />
        </el-form-item>
        <el-form-item label="企业建筑面积" prop="structureArea">
          <el-input v-model="form.structureArea" placeholder="请输入企业建筑面积" />
        </el-form-item>
        <el-form-item label="危险化学品库房面积" prop="chemistryArea">
          <el-input v-model="form.chemistryArea" placeholder="请输入危险化学品库房面积" />
        </el-form-item>
        <el-form-item label="储罐和容器总容积" prop="containerArea">
          <el-input v-model="form.containerArea" placeholder="请输入储罐和容器总容积" />
        </el-form-item>
        <el-form-item label="应急服务电话" prop="emergencyPhone">
          <el-input v-model="form.emergencyPhone" placeholder="请输入应急服务电话" />
        </el-form-item>
        <el-form-item label="安全值班电话" prop="securityPhone">
          <el-input v-model="form.securityPhone" placeholder="请输入安全值班电话" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFactoryInfo, getFactoryInfo, delFactoryInfo, addFactoryInfo, updateFactoryInfo } from "@/api/factorymanagement/factoryInfo";

export default {
  name: "FactoryInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业信息登记表格数据
      factoryInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        area: null,
        address: null,
        isInPark: null,
        socialId: null,
        workerNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        area: [
          { required: true, message: "地区不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "工商注册地址不能为空", trigger: "blur" }
        ],
        bussinessScope: [
          { required: true, message: "营业执照经营范围不能为空", trigger: "blur" }
        ],
        isInPark: [
          { required: true, message: "是否在化工园区内不能为空", trigger: "blur" }
        ],
        socialId: [
          { required: true, message: "统一社会信用代码不能为空", trigger: "blur" }
        ],
        workerNum: [
          { required: true, message: "企业人数不能为空", trigger: "blur" }
        ],
        mainProduct: [
          { required: true, message: "主要产品及生产规模不能为空", trigger: "blur" }
        ],
        mainChemistry: [
          { required: true, message: "主要进口化学品不能为空", trigger: "blur" }
        ],
        structureArea: [
          { required: true, message: "企业建筑面积不能为空", trigger: "blur" }
        ],
        chemistryArea: [
          { required: true, message: "危险化学品库房面积不能为空", trigger: "blur" }
        ],
        containerArea: [
          { required: true, message: "储罐和容器总容积不能为空", trigger: "blur" }
        ],
        emergencyPhone: [
          { required: true, message: "应急服务电话不能为空", trigger: "blur" }
        ],
        securityPhone: [
          { required: true, message: "安全值班电话不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询企业信息登记列表 */
    getList() {
      this.loading = true;
      listFactoryInfo(this.queryParams).then(response => {
        this.factoryInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        area: null,
        address: null,
        bussinessScope: null,
        isInPark: null,
        socialId: null,
        workerNum: null,
        mainProduct: null,
        mainChemistry: null,
        structureArea: null,
        chemistryArea: null,
        containerArea: null,
        emergencyPhone: null,
        securityPhone: null,
        userId: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业信息登记";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFactoryInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业信息登记";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFactoryInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFactoryInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除企业信息登记编号为"' + ids + '"的数据项？').then(function() {
        return delFactoryInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('factorymanagement/factoryInfo/export', {
        ...this.queryParams
      }, `factoryInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>